package tech.lq0.mineapex.armor;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import tech.lq0.mineapex.group.ApexGroup;
import tech.lq0.mineapex.material.EvoArmorMaterial;
import tech.lq0.mineapex.registry.ItemRegistry;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;

public class EvoShield extends ArmorItem {
    public static final int[] damage = new int[]{0, 150, 450, 1200};
    public static final int[] shield = new int[]{50, 75, 100, 125};


    public EvoShield() {
        super(EvoArmorMaterial.EVO, EquipmentSlotType.CHEST, new Properties().group(ApexGroup.group));
    }

    public static int getLevel(ItemStack stack) {
        // 非进化护盾
        if (!stack.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
            return 0;
        }
        // 无tag
        if (stack.getChildTag("evo") == null) {
            return 0;
        }

        int level = 0;
        float damage = stack.getOrCreateChildTag("evo").getFloat("damage");

        for (int i = EvoShield.damage.length - 1; i >= 0; i--) {
            if (damage >= EvoShield.damage[i]) {
                level = i;
                break;
            }
        }
        return level;
    }

    @Override
    @ParametersAreNonnullByDefault
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        float damage, max, current;
        if (stack.getChildTag("evo") == null) {
            damage = 0;
            max = shield[0];
            current = shield[0];
        } else {
            damage = stack.getOrCreateChildTag("evo").getFloat("damage");
            max = stack.getOrCreateChildTag("evo").getFloat("max");
            current = stack.getOrCreateChildTag("evo").getFloat("current");
        }
        tooltip.add(new StringTextComponent("Damage: " + damage));
        tooltip.add(new StringTextComponent("Max: " + max));
        tooltip.add(new StringTextComponent("Current: " + current));
        if (stack.getOrCreateChildTag("evo").getBoolean("noevo")) {
            tooltip.add(new StringTextComponent("NoEvo"));
        }
    }

    @Override
    @ParametersAreNonnullByDefault
    public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) {
        NonNullList<ItemStack> shields = NonNullList.create();
        if (group == ApexGroup.group) {
            for (int i = 0; i < damage.length; i++) {
                float d = damage[i];
                float s = shield[i];
                ItemStack stack = new ItemStack(ItemRegistry.EVO_SHIED.get(), 1);
                CompoundNBT evo = stack.getOrCreateChildTag("evo");
                evo.putFloat("damage", d);
                evo.putFloat("max", s);
                evo.putFloat("current", s);
                shields.add(stack);
            }
            ItemStack stack = new ItemStack(ItemRegistry.EVO_SHIED.get(), 1);
            CompoundNBT evo = stack.getOrCreateChildTag("evo");
            evo.putFloat("damage", Float.POSITIVE_INFINITY);
            evo.putFloat("max", Float.POSITIVE_INFINITY);
            evo.putFloat("current", Float.POSITIVE_INFINITY);
            shields.add(stack);

            items.addAll(shields);

            // 非进化护盾
            for (ItemStack i : shields) {
                ItemStack copy = i.copy();
                copy.getOrCreateChildTag("evo").putBoolean("noevo", true);
                items.add(copy);
            }

        }
    }
}